//
//  NSObject+EventBus.m
//  Express
//
//  Created by lhongl on 2025/9/25.
//  Copyright © 2025 QM. All rights reserved.
//

#import "NSObject+EventBus.h"
#import <objc/runtime.h>
static const BOOL observerListMapKey = NO;
@interface NSObject ()

///
@property (nonatomic, strong) NSMutableDictionary  *observerMap;

@end
@implementation NSObject (EventBus)

#pragma mark 注册
- (void)registerEventBusWithName:(NSString *)name completion:(void(^)(NSDictionary  *info))completion{
    NSAssert(name, @"[MG_DEBUG] name不能为空");
    id observer = [[NSNotificationCenter defaultCenter] addObserverForName:name object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification * _Nonnull note) {
        if(completion){
            completion(note.userInfo);
        }
    }];
    if(observer != NULL){
        [self.observerMap setValue:observer forKey:name];
    }
}

- (void)registerAsyncEventBusWithName:(NSString *)name completion:(void(^)(NSDictionary  *info))completion{
    NSAssert(name, @"[MG_DEBUG] name不能为空");
    id observer = [[NSNotificationCenter defaultCenter] addObserverForName:name object:nil queue:[[NSOperationQueue alloc] init] usingBlock:^(NSNotification * _Nonnull note) {
        if(completion){
            completion(note.userInfo);
        }
    }];
    if(observer != NULL){
        [self.observerMap setValue:observer forKey:name];
    }
}


#pragma mark 发送
- (void)postEventBusWithName:(NSString *)name{
    [self postEventBusWithName:name info:@{}];
}

- (void)postEventBusWithName:(NSString *)name info:(NSDictionary * _Nullable)info{
    NSAssert(name, @"[MG_DEBUG] name不能为空");
    [[NSNotificationCenter defaultCenter] postNotificationName:name object:nil userInfo:info];
}

- (void)postAsyncEventBusWithName:(NSString *)name{
    [self postAsyncEventBusWithName:name info:@{}];
}

- (void)postAsyncEventBusWithName:(NSString *)name
                             info:(NSDictionary * _Nullable)info{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        [self postEventBusWithName:name info:info];
    });
}

#pragma mark 删除
- (void)removeEventBusWithName:(NSString * _Nullable)name{
    if (name == nil || name == NULL || name.length == 0) {
        [self removeAllEventBus];
        return;
    }
    id observer = self.observerMap[name];
    if(observer != NULL){
        [[NSNotificationCenter defaultCenter] removeObserver:observer name:name object:nil];
        observer = NULL;
    }
}

- (void)removeAllEventBus{
    [self.observerMap enumerateKeysAndObjectsUsingBlock:^(id  _Nonnull key, id  _Nonnull obj, BOOL * _Nonnull stop) {
        [[NSNotificationCenter defaultCenter] removeObserver:obj];
        obj = NULL;
    }];
}

#pragma mark
- (void)setObserverMap:(NSMutableDictionary *)observerMap{
    objc_setAssociatedObject(self, &observerListMapKey, observerMap, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (NSMutableDictionary *)observerMap{
    NSMutableDictionary *info = objc_getAssociatedObject(self, &observerListMapKey);
    if(!info){
        info = [NSMutableDictionary dictionary];
        self.observerMap = info;
        return info;
    }
    return info;
}



@end
